package app.com.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.SearchView.OnQueryTextListener;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import adapter.ClickListener;
import adapter.DividerItemDecoration;
import adapter.RecyclerTouchListener;
import adapter.RecyclerViewAdapter;


public class MainActivity extends ActionBarActivity implements OnQueryTextListener {
    private Toolbar toolbar;
    private RecyclerView recyclerView;
    private RecyclerViewAdapter recyclerViewAdapter;
    private SearchView searchView;

    private String[] countryNames = {"Russia", "Turkmenistan", "USA", "Australia", "Malaysia", "Spain", "Italy", "Uzbekistan", "Canada", "Japan", "Nepal", "India"};
    private String[] countryCapitals = {"Moscow", "Ashgabat", "Washington", "Canberra", "Kuala-Lumpur", "Madrid", "Rome", "Tashkent", "Ottawa", "Tokyo", "Kathmandu", "New Delhi"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);

        RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerViewAdapter = new RecyclerViewAdapter(this, getData());
        recyclerView.setAdapter(recyclerViewAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(itemDecoration);
        recyclerView.addOnItemTouchListener(new RecyclerTouchListener(this, recyclerView, new ClickListener() {
            @Override
            public void onClick(View view, int position) {
                System.out.println("TAG onClick " + position + " country name " + countryNames[position]);
                //System.out.println("TAG onClick pos " + recyclerViewAdapter.getItemId(position));

                Intent intent = new Intent(MainActivity.this, SubActvity.class);
                intent.putExtra("countryName", countryNames[position]);
                startActivity(intent);
            }

            @Override
            public void onLongClick(View view, int position) {
                System.out.println("TAG onLongClick " + position);
            }
        }));
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);

        MenuItem searchItem = menu.findItem(R.id.search_lyrics);
        searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        searchView.setQueryHint(getString(R.string.queryHint));
        searchView.setOnQueryTextListener(this);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        /*if (id == R.id.action_settings) {
            return true;
        }*/

        /*if (id == R.id.test2) {
            Intent intent = new Intent(this, SubActvity.class);
            intent.putExtra("countryName", "Turkmenistan russia uzbekistan");
            startActivity(intent);
        }*/

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        System.out.println("TAG on resume");
        toolbar.getBackground().setAlpha(1000);
    }

    private List<Country> getData() {
        List<Country> countryList = new ArrayList<Country>();

        for (int i = 0; i < countryNames.length; i++) {
            countryList.add(new Country(countryNames[i], countryCapitals[i]));
        }

        return countryList;
    }

    @Override
    public boolean onQueryTextSubmit(String s) {
        Log.i("searchView submit", s);
        searchView.clearFocus();
        return false;
    }

    @Override
    public boolean onQueryTextChange(String s) {
        Log.i("searchView change", s);
        recyclerViewAdapter.getFilter().filter(s);
        return false;
    }
}